const express  = require ('express');
const pool = require('../pool');
const session = require('express-session');
const { query } = require('express');
const router = express.Router();

router.get('/queryAll',(req,res,next)=>{
    console.log(req.query.type)
    let type = req.query.type;
    let id = req.query.id;
    pool.query(`select * from card where orderState = ${type} and userId=${id}`,(err,r)=>{
        if(err){
            return next(err)
        }
        if(r.length > 0){
            res.send({code:200,data:r})
        }else{
            res.send({code:400,data:'暂无订单'})
        }
    })
})

router.post('/submit',(req,res,next)=>{
    let id = req.body.id
    let type = req.body.type
    let uid = req.body.uid
    pool.query(`update card set orderState = ${type} where userId = ${uid} and id=${id}`,(err,r)=>{
        if(err){
            return next(err)
        }
        console.log(r)
        if(r.affectedRows>0){
            res.send({code:200,msg:'操作成功'})
        }else{
            res.send({code:400,msg:'操作失败'})
        }
    })
})

router.post('/delt',(req,res,next)=>{
    let id = req.body.id
    let uid= req.body.uid
    pool.query(`delete from card where id=${id} and userId = ${uid}`,(err,r)=>{
        if(err){
            return next(err)
        }
        if(r.affectedRows>0){
            res.send({code:200,msg:'操作成功'})
        }else{
            res.send({code:400,msg:'操作失败'})
        }
    })
})

module.exports = router;